#!/usr/bin/python2
# coding=utf-8

"""
    Ball Fall - a 2d arcade game written in Python
    Copyright (C) 2008 Bartosz Gąsiorzewski <g.bartek@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""

from time import time

class Timer:
    interval = None
    start_time = None
    last = None
    
    def __init__(self, interval=None):
        self.interval = float(interval)
    
    def start(self):
        self.start_time = time()
        self.last = self.start_time
    
    def getElapsed(self):
        """Returns an integer telling how many intervals have passed
        since last call or since calling start()."""
        now = time()
        total_elapsed = (now - self.start_time) / self.interval
        now_elapsed = int((now - self.last) / self.interval)
        if now_elapsed > 0:
            diff = now - self.start_time
            now_rounded = diff - diff % self.interval
            self.last = self.start_time + now_rounded
        return now_elapsed
